package com.whfc.wxmp.api.sys.controller;

import com.whfc.base.service.SmsService;
import com.whfc.common.result.ListData;
import com.whfc.common.result.R;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.fuum.dto.WxRuleDTO;
import com.whfc.fuum.dto.WxUserDTO;
import com.whfc.fuum.entity.WxUser;
import com.whfc.fuum.param.WxUserParam;
import com.whfc.wxmp.api.sys.service.WxUsersService;
import com.whfc.wxmp.config.AbstractBaseController;
import com.whfc.wxmp.dto.UserDetailDTO;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/22 15:28
 */
@RestController
@RequestMapping("/mp/api/user")
public class WxUserController extends AbstractBaseController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private WxUsersService wxUserService;

    @DubboReference(interfaceClass = SmsService.class, version = "1.0.0")
    private SmsService smsService;

    @GetMapping("info")
    public Result getUserInfo(@CurrentWxUser WxUser loginUser) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询用户信息", RequestConetxtUtil.getParameters());
        WxUserDTO wxUser = wxUserService.getUserById(loginUser.getId());
        return ResultUtil.success(wxUser);
    }

    @PostMapping("update/name")
    public Result updateUserName(@CurrentWxUser WxUser loginUser, @RequestParam("name") String name) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "修改用户姓名", RequestConetxtUtil.getParameters());
        wxUserService.updateUserName(loginUser, name);
        return ResultUtil.success(name);
    }

    @PostMapping("update/phone")
    public Result updateUserPhone(@CurrentWxUser WxUser loginUser, @RequestParam("phone") String phone, @RequestParam("code") String code) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "修改用户手机号", RequestConetxtUtil.getParameters());
        //修改手机号
        wxUserService.updateUserPhone(loginUser, phone, code);
        return ResultUtil.success();
    }


    @GetMapping("/rule/list")
    public Result ruleList(@CurrentWxUser WxUser loginUser) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取用户权限列表", "{}");
        List<WxRuleDTO> ruleList = wxUserService.getUserRuleList(loginUser);
        return ResultUtil.success(new ListData<>(ruleList));
    }


    @GetMapping(value = "/rule/list", params = {"deptId"})
    public Result ruleList(@CurrentWxUser WxUser loginUser, @RequestParam("deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取用户权限列表", RequestConetxtUtil.getParameters());
        List<WxRuleDTO> ruleList = wxUserService.getUserRuleList(loginUser, deptId);
        return ResultUtil.success(new ListData<>(ruleList));
    }

    /**
     * 2.0.0修改用户信息(昵称/头像/密码)
     *
     * @param param
     * @return
     */
    @PostMapping("/info/edit")
    public R editUserInfo(@RequestBody @Validated WxUserParam param) {
        logger.info("更新用户信息:{}", param);

        WxUser wxUser = getLoginUser(request);

        param.setWxUserId(wxUser.getId());
        wxUserService.editUserInfo(param);

        if (StringUtils.isNotBlank(param.getName())) {
            wxUser.setName(param.getName());
        }
        if (StringUtils.isNotBlank(param.getAvatarUrl())) {
            wxUser.setAvatarUrl(param.getAvatarUrl());
        }
        SecurityUtils.getSubject().getSession().setAttribute(SessionAttr.WXMP_USER, wxUser);
        return R.success();
    }

    /**
     * 2.0.0我的主页详情
     *
     * @param deptId 组织ID
     * @return
     */
    @GetMapping("/prj/detail")
    public R<UserDetailDTO> getPrjDetail(@CurrentWxUser WxUser loginUser,@RequestParam Integer deptId) {
//        Integer wxUserId = getLoginUser().getId();
        logger.info("查询我的(项目)详情:deptId={},wxUserId={}", deptId, getLoginUser(request).getId());
        String phone = getLoginUser(request).getPhone();
//        UserDetailDTO res = wxUserService.getPrjDetail(deptId, wxUserId, phone);
        UserDetailDTO res = wxUserService.getPrjDetail(deptId, loginUser.getId(), phone);
        return R.success(res);
    }

}
